<?php
    /**
     * PushMessage.php
     *
     * Created by PhpStorm.
     * author: liuml  <liumenglei0211@163.com>
     * DateTime: 2018/7/27  18:10
     */

    namespace app\wxapi\controller\v1;

    use data\model\TzOrderModel;
    use think\Request;
    use think\Config;
    use data\service\PushMessage as PMSG;

    class PushMessage extends ApiController
    {
        /**
         * 对外api接口发送模板消息
         * @return \think\response\Json
         */
        public function createMessage()
        {
            try {
                $pmsg         = new PMSG();
                $request      = Request::instance();
                $data         = $request->post();
                $template_key = $data['type'] ? : '';
                if (empty($template_key)) {
                    return static::runResponse(0, '参数错误');
                }
                switch ($template_key) {
                    case 'order':
                        $res = $pmsg->saveTzOrder($data);
                        break;
                    default:
                        $user     = new User();
                        $userInfo = $user->getUserInfoData($data['open_id']);
                        if (!empty($userInfo) && $userInfo['user_status'] > 0) {
                            $res = $pmsg->sendTemp($data);
                        }
                }
                if ($res) {
                    return static::runResponse(1, '操作成功');
                }
                return static::runResponse(0, '操作失败');
            } catch (\Exception $e) {
                abort($e->getCode(), $e->getMessage());
            }
        }

        /**
         * 打印日志
         * @param $type
         */
        public function printLog($type)
        {

            /*$tzOrder = new \data\model\TzOrderModel();
            $arr     = $tzOrder->where(['shop_name' => ['eq', '天择信上']])->order('id desc')->select();
            $arr     = collection($arr)->toArray();
            print_r($arr);
            die;*/

            $pushService            = new PMSG();
            $data                   = $pushService->getMessageLog($type);
            $data['request_header'] = json_decode(htmlspecialchars_decode($data['request_header']), true);
            $data['request_body']   = json_decode(htmlspecialchars_decode($data['request_body']), true);
            $data['response']       = json_decode(htmlspecialchars_decode($data['response']), true);
            print_r($data);
            die;
        }

    }